<?php
ini_set("display_errors",1);
include_once "func.php";

   
if($_FILES["file"]["error"] > 0){
       echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}else{ 
    $newfilename = time().'.jpg';
  
    $res = move_uploaded_file($_FILES["file"]["tmp_name"], "uploadfile/" . $newfilename);
    $filepath = 'uploadfile/' . $newfilename;

    //oriental
    //image_fix_orientation($filepath);
    //crop
    //imagecropper($filepath,120,120);

    getsmallimg($filepath);

    //mark
    //mark_pic($filepath,'uploadfile/water.png',0,0);

     $return = array(
        'filepath' => '/'.$filepath
     );
     ajax_return($return);
}

function getsmallimg($source_path){
      //因为PHP只能对资源进行操作，所以要对需要进行缩放的图片进行拷贝，创建为新的资源
    $src=imagecreatefromjpeg($source_path);
    
    //取得源图片的宽度和高度
    $size_src=getimagesize($source_path);
    $w=$size_src['0'];
    $h=$size_src['1'];
    
    //指定缩放出来的最大的宽度（也有可能是高度）
    $max=1000;
    
    //根据最大值为300，算出另一个边的长度，得到缩放后的图片宽度和高度
    if($w > $h){
        $w=$max;
        $h=$h*($max/$size_src['0']);
    }else{
        $h=$max;
        $w=$w*($max/$size_src['1']);
    }
    
    //声明一个$w宽，$h高的真彩图片资源
    $image=imagecreatetruecolor($w, $h);
    
    
    //关键函数，参数（目标资源，源，目标资源的开始坐标x,y, 源资源的开始坐标x,y,目标资源的宽高w,h,源资源的宽高w,h）
    imagecopyresampled($image, $src, 0, 0, 0, 0, $w, $h, $size_src['0'], $size_src['1']);
    
    imagejpeg($image,$source_path);
    
    //销毁资源
    imagedestroy($image);

}


function image_fix_orientation($filename) {
    $exif = exif_read_data($filename);
    $source = imagecreatefromjpeg($filename);

    if (!empty($exif['Orientation'])) {
        switch ($exif['Orientation']) {
            case 3:
                $image = imagerotate($source, 180, 0);imagejpeg($image,$filename);
                break;

            case 6:
                $image = imagerotate($source, -90, 0);imagejpeg($image,$filename);
                break;

            case 8:
                $image = imagerotate($source, 90, 0);imagejpeg($image,$filename);
                break;
        }
        
    }

}


function imagecropper($source_path, $target_width, $target_height)
      {
        $source_info   = getimagesize($source_path);
        $source_width  = $source_info[0];
        $source_height = $source_info[1];



        $source_mime   = $source_info['mime'];
        $source_ratio  = $source_height / $source_width;
        $target_ratio  = $target_height / $target_width;

        // 源图过高
        if ($source_ratio > $target_ratio)
        {
          $cropped_width  = $source_width;
          $cropped_height = $source_width * $target_ratio;
          $source_x = 0;
          $source_y = ($source_height - $cropped_height) / 2;
        }
        // 源图过宽
        elseif ($source_ratio < $target_ratio)
        {
          $cropped_width  = $source_height / $target_ratio;
          $cropped_height = $source_height;
          $source_x = ($source_width - $cropped_width) / 2;
          $source_y = 0;
        }
        // 源图适中
        else
        {
          $cropped_width  = $source_width;
          $cropped_height = $source_height;
          $source_x = 0;
          $source_y = 0;
        }

        switch ($source_mime)
        {
          case 'image/gif':
            $source_image = imagecreatefromgif($source_path);
            break;

          case 'image/jpeg':
            $source_image = imagecreatefromjpeg($source_path);
            break;

          case 'image/png':
            $source_image = imagecreatefrompng($source_path);
            break;

          default:
            return false;
            break;
        }

        $target_image  = imagecreatetruecolor($target_width, $target_height);
        $cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);

        // 裁剪
        imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height); 
        // 缩放
        imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);

         
        imagejpeg($target_image,$source_path);
        imagedestroy($source_image);
        imagedestroy($target_image);
        imagedestroy($cropped_image);
      }



?>